package com.luke.dubbo.order.service.impl;

import com.alibaba.dubbo.rpc.RpcContext;
import com.luke.dubbo.api.bean.UserAddress;
import com.luke.dubbo.api.service.OrderService;
import com.luke.dubbo.api.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.concurrent.Future;

@Service
public class OrderServiceImpl implements OrderService {

  @Autowired
  private UserService userService;

  @Override
  public List<UserAddress> initOrder(String userId) {
      int userIdInt = Integer.valueOf(userId);
      for(int i = userIdInt; i < 5; i++){
        try {
          //设置隐式参数
          RpcContext.getContext().setAttachment("index",i+"");
          //查询用户收货地址

          List<UserAddress> userAddressList = userService.getUserAddressList(i+"");

          //Future<Object> future = RpcContext.getContext().getFuture();
          //userAddressList = (List<UserAddress>)future.get(); //获取异步调用结果

          System.out.println("userAddressList:"+userAddressList);
          if(userAddressList != null){
            userAddressList.forEach((userAddress -> {
              System.out.println("RESPONSE FROM 【"+userAddress.getIp()+"】：address：" +
                      ""+userAddress.getUserAddress()+",userId："+userAddress.getUserId());
            }));
            //输出远程调用的目标地址
            String serverIP = RpcContext.getContext().getRemoteHost();
            System.out.println("serverIP:"+serverIP);
          }

          Thread.sleep(1_000);

        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    return null;
  }

}
